package cn.ppwx.crm.common.utils;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;

public class ClassScanProvider {
	
	public static <E> List<Class<E>> getSubClasses(final Class<E> parentClass,
			final String packagePath) throws Exception {
		final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
				false);
		provider.addIncludeFilter(new AssignableTypeFilter(parentClass));
		final Set<BeanDefinition> components = provider
				.findCandidateComponents(packagePath);
		final List<Class<E>> subClasses = new ArrayList<Class<E>>();
		for (final BeanDefinition component : components) {

			@SuppressWarnings("unchecked")
			final Class<E> cls = (Class<E>) Class.forName(component
					.getBeanClassName());
			if (Modifier.isAbstract(cls.getModifiers())) {
				continue;
			}
			subClasses.add(cls);
		}
		return subClasses;
	}
}
